<?php
class Administration extends Controller
{
    function __construct()
    {
       parent::controller();
    }

    function Administration()
    {
        self::__construct();
    }

    function index()
    {
        if($this->quickauth->logged_in())
        {
            $this->dashboard();
        }
        else
        {
            $this->login();
        }
    }
    
    function login()
	{
        if($this->input->post('login'))
        {
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            $this->quickauth->login($username, $password, 'administration/dashboard');
        }
        else
        {
            $this->load->view('header');
            $this->load->view('login');
            $this->load->view('footer');
        }
	}

	function dashboard()
	{
		$this->quickauth->restrict('admin');
        $header = array('body_id' => 'dashboard');

        $this->load->model('mdl_administration_main');
        $dashboard['recent'] = $this->mdl_administration_main->populate_dashboard();

        $this->load->view('header', $header);
        $this->load->view('administration/dashboard', $dashboard);
        $this->load->view('footer');
	}

    function settings()
    {
        $this->quickauth->restrict('admin');

        $this->form_validation->set_rules('sitename', 'alpha_dash|max_length[60]');
        $this->form_validation->set_rules('siteurl', 'alpha_dash|max_length[60]');
        $this->form_validation->set_rules('administrator_name', 'alpha_dash|max_length[60]');
        $this->form_validation->set_rules('administrator_email', 'valid_email|max_length[60]');
        $this->form_validation->set_rules('system_email', 'valid_email|max_length[60]');
        $this->form_validation->set_rules('contact_email', 'valid_email|max_length[60]');

        if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('header');
            $this->load->view('edit_settings');
            $this->load->view('footer');
		}
		else
		{
			$sitename = $this->input->post('sitename');
			$siteurl  = $this->input->post('siteurl');
			$administrator_name = $this->input->post('administrator_name');
            $administrator_email = $this->input->post('administrator_email');
			$system_email = $this->input->post('system_email');
			$contact_email = $this->input->post('contact_email');

            $site_data = array(
                'sitename' => $sitename,
                'siteurl' => $siteurl,
                'administrator_name' => $administrator_name,
                'administrator_email' => $administrator_email,
                'system_email' => $system_email,
                'contact_email' => $contact_email
                );

            $this->db->update('configuration', $site_data);

            $recent = array(
                            'username' => $_SESSION['username'],
                            'module' => 'core',
                            'action' => 'Updated site information'
                            );

            $this->db->insert('recent', $recent);


            $data = array('body_id' => 'contact');

            $data2 = array(
                            'title' => 'Site Information Updated',
                            'message' => 'Congratulations, your site information has been updated.'
                        );

            $this->load->view('header', $data);
            $this->load->view('message', $data2);
            $this->load->view('footer');
		}
    }

	function logout()
	{
		$this->quickauth->logout();
		show_error('You have been logged out. '.anchor('', 'Return to the Site.'));
	}

}